<template>
  <div>
    <!-- 下拉框 -->
    <el-row type="flex" justify="end">
      <el-select v-model="year" size="small" @change="dateChangeFn">
        <el-option
          v-for="item in yearList"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select v-model="month" size="small" @change="dateChangeFn">
        <el-option
          v-for="item in 12"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
    </el-row>
    <!-- 日历 -->
    <el-calendar v-if="showCalendar" v-model="curDate">
      <!-- dateCell是插槽名 -->
      <!-- <template v-slot:dateCell="{date, data}">
        {{ data.day | formatDateTwo }}
        <span v-if="isWeeik(date)">休</span>
      </template> -->
      <template v-slot:dateCell="{ date, data }" class="content">
        <div class="date-content">
          <span class="text"> {{ data.day | formatDateTwo }}</span>
          <span v-if="isWeeik(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
const curDate = new Date() // 获取当前日期
const year = curDate.getFullYear() // 获取当前年份
const month = curDate.getMonth() + 1 // 获取当前月份
// 生成前后5年的年份列表
const yearList = []
for (let i = 0; i < 11; i++) {
  yearList.push(year - 5 + i)
}

export default {
  filters: {
    // 格式化日历的时间
    formatDateTwo(val) {
      return parseInt(val.split('-')[2])
    }
  },
  data() {
    return {
      curDate: curDate,
      year,
      month,
      yearList,
      showCalendar: true
    }
  },
  watch: {
    // 日历关联下拉框年月
    curDate(date) {
      this.year = date.getFullYear()
      this.month = date.getMonth() + 1
    },
    // 监听语言的变化，watch可以监听对象中的某个属性，语法如下
    '$i18n.locale'() {
      // 解决语言变化的时候，日历组件不能实时更新语言的问题
      this.showCalendar = false
      this.$nextTick(() => {
        this.showCalendar = true
      })
    }
  },
  methods: {
    // 下拉选项年月变化，关联日历组件
    dateChangeFn() {
    //   console.log(this.year, this.month)
      this.curDate = new Date(`${this.year}-${this.month}`)
    },
    // 判断周六日
    isWeeik(date) {
      // getDay返回0表示周日，返回6表示周六
      return date.getDay() === 0 || date.getDay() === 6
    }
  }
}
</script>

<style scoped>
 /deep/ .el-calendar-day {
  height:  auto;
 }
 /deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 /deep/ .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 /deep/ .el-calendar__header {
   display: none
 }
</style>
